class Driver < ActiveRecord::Base
  attr_accessible :city_id, :contact_no_1, :contact_no_2, :contact_no_3, :email, :fax_no, :name, :postal_code, :state_id, :street, :unit

  has_and_belongs_to_many :addresses, :join_table => 'drivers_addresses'
  has_and_belongs_to_many :contacts, :join_table => 'drivers_contacts'

  has_many :licenses

  accepts_nested_attributes_for :addresses

  def self.delete_address(driver_id, address_id)
    begin
      Driver.transaction do
        driver = Driver.find(driver_id)
        address = Address.find(address_id)
        driver.addresses.destroy(address)
        address.destroy
      end
    rescue
      return false
    end
    return true
  end
  
  def self.delete_contact(driver_id, contact_id)
    begin
      Driver.transaction do
        driver = Driver.find(driver_id)
        contact = Contact.find(contact_id)
        driver.contacts.destroy(contact)
        contact.destroy
      end
    rescue
      return false
    end
    return true
  end
end
